package com.jp.knowledge.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.ImageActivity;
import com.jp.knowledge.adapter.CollectionCommtemAdapter;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.model.HeadLineData;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.ProductUIModel;
import com.jp.knowledge.my.adapter.CollectionUIAdapter;
import com.jp.knowledge.my.adapter.UiFragmentAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/1/21.
 */

public class CollectionNewsUiFragment extends CollectionNewsFragment implements UiFragmentAdapter.CollectionCallback {

	@Override
	protected void initView() {
		super.initView();
		contentView.setBackgroundColor(getResources().getColor(R.color.gray_bg));
		contentRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				((StaggeredGridLayoutManager) layoutManager).invalidateSpanAssignments();
			}
		});
	}

	@Override
	protected RecyclerView.Adapter initAdapter(List datas) {
		return new CollectionUIAdapter(mContext, datas);
	}

	@Override
	protected RecyclerView.LayoutManager initLinearLayoutManager() {
		StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
		layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
		((CollectionUIAdapter) adapter).addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
			@Override
			public void itemSelect(int position) {
				onItemClick(position);
			}
		});
		((CollectionUIAdapter) adapter).setCollectionCallback(this);
		return layoutManager;
	}

	@Override
	protected void onItemClick(int position){
		ImageActivity.startActivity(mContext, position, -1, datas);
	}

	@Override
	protected Class getDatasType() {
		return ProductUIModel.class;
	}

	@Override
	public void collectionOperateSuccess(int position) {
		datas.remove(position);
		adapter.notifyItemRemoved(position);
	}

	@Override
	public void collectionOperateError(int position) {}

	@Override
	protected void onBroadCastReceive(String action, Intent intent){
		if(!LocalBroadCast.COMM_CHANGE_SIZE.equals(action))
			super.onBroadCastReceive(action, intent);
	}

	protected boolean isSameItem(Object data, String id){
		return (id != null && data != null && id.equals(((ProductUIModel)data).getId()));
	}
}
